package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageDate;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.MulticastChannel;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result uplode(@RequestParam("excelFile") MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename ();
        if (StringUtils.isEmpty (originalFilename)) {
            return new Result (false, "文件名不存在");
        }
        //读取excel文件信息
        InputStream inputStream = multipartFile.getInputStream ();
        Workbook book = null;
        try {
            book = new XSSFWorkbook (inputStream);
        } catch (Exception ex) {
            book = new HSSFWorkbook (new FileInputStream (String.valueOf (inputStream)));
        }

        //遍历读取数据
        List<OrderSetting> orderSettings = new ArrayList<> ();
        for (Sheet rows : book) {
            for (int i = 1; i < rows.getLastRowNum (); i++) {
                Row row = rows.getRow (i);
                OrderSetting orderSetting = parseOrderSetting (row);
                orderSettings.add (orderSetting);
            }
        }
        //数据入库
        orderSettingService.addAll (orderSettings);
        return new Result (true, MessageConst.ACTION_SUCCESS);
    }

    //从excel中提取相关数据
    private OrderSetting parseOrderSetting(Row row) {
        Date date = null;
        Integer i = null;
        //得到日期
        if (null != row.getCell (0)) {
            date = row.getCell (0).getDateCellValue ();
        }
        //得到最大预约数量
        if (null != row.getCell (1)) {
            double numericCellValue = row.getCell (1).getNumericCellValue ();
            //double转换int
            i = Double.valueOf (numericCellValue).intValue ();
        }

        return new OrderSetting (date, i);
    }
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("year")Integer year,@RequestParam("month")Integer month) throws ParseException {
        log.info( "[根据月份查所有]year:{},month:{}",year,month);
        Result orderSettingByMonth = orderSettingService.getOrderSettingByMonth(year, month);
        return orderSettingByMonth;



    }



}
